package com.neusoft.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.neusoft.bean.AdminInfoBean;
import com.neusoft.dao.AdminInfoDao;

public class TestDriver {

	public static void main(String[] args) {
		//test01();
		test04();
	}
	
	
	private static void test04() {
		
		//使用dao层方法进行查询操作
		AdminInfoBean adminB = getMyMapper(AdminInfoDao.class).selectAdminInfoById2(1);
		if (adminB!=null) {
			System.out.println(adminB.toString());
		}
	}
	
	
	private static void test03() {
		SqlSession sqlS = getSqlSession();
		//获取dao层接口的实例对象
		AdminInfoDao adminD = sqlS.getMapper(AdminInfoDao.class);
		//使用dao层方法进行查询操作
		List<AdminInfoBean> lst = adminD.selectAdminInfoById(1);
		for (AdminInfoBean aib : lst) {
			System.out.println(aib.toString());
		}
	}
	
	
	/**
	 * 把获取sqlsession对象封装成一个方法getSqlSession()
	 * 
	 */
	private static void test02() {
		SqlSession sqlS = getSqlSession();
		List<AdminInfoBean> lst = sqlS.selectList("selectAdminInfoById", 1);
		for (AdminInfoBean aib : lst) {
			System.out.println(aib.toString());
		}
	}
	/**
	 * 获取SqlSession对象
	 * @return SqlSession对象
	 */
	private static SqlSession getSqlSession() {
		SqlSession sqlS = null;
		try {
			//将mybatis配置文件读取到流中
			InputStream isConfigMybatis = TestDriver.class.getClassLoader().getResourceAsStream("config-mybatis.xml");
			//通过properties工具类加载数据库配置文件
			Properties prop = new Properties();
			prop.load(TestDriver.class.getClassLoader().getResourceAsStream("db.properties"));
			//SqlSession的创建类
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			//使用builder创建SqlSessionFactory类
			SqlSessionFactory factory = builder.build(isConfigMybatis,prop);
			//获取sqlSession对象
			sqlS = factory.openSession();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sqlS;
	}
	
	
	
	/**
	 * 获取dao层接口的实例对象
	 * @param clazz
	 * @return dao层接口的实例对象
	 */
	private static <T> T getMyMapper(Class<T> clazz) {
		T re_t = null;
		try {
			//将mybatis配置文件读取到流中
			InputStream isConfigMybatis = TestDriver.class.getClassLoader().getResourceAsStream("config-mybatis.xml");
			//通过properties工具类加载数据库配置文件
			Properties prop = new Properties();
			prop.load(TestDriver.class.getClassLoader().getResourceAsStream("db.properties"));
			//SqlSession的创建类
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			//使用builder创建SqlSessionFactory类
			SqlSessionFactory factory = builder.build(isConfigMybatis,prop);
			//获取sqlSession对象
			SqlSession sqlS = factory.openSession();
			re_t = sqlS.getMapper(clazz);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return re_t;
	}
	
	
	
	private static void test01() {
		try {
			//将mybatis配置文件读取到流中
			InputStream isConfigMybatis = TestDriver.class.getClassLoader().getResourceAsStream("config-mybatis.xml");
			//通过properties工具类加载数据库配置文件
			Properties prop = new Properties();
			prop.load(TestDriver.class.getClassLoader().getResourceAsStream("db.properties"));
			//SqlSession的创建类
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			//使用builder创建SqlSessionFactory类
			SqlSessionFactory factory = builder.build(isConfigMybatis,prop);
			//获取sqlSession对象
			SqlSession sqlS = factory.openSession();
			List<AdminInfoBean> lst = sqlS.selectList("selectBlog", 1);
			for (AdminInfoBean aib : lst) {
				System.out.println(aib.toString());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
